using Habanero.Base;

namespace FireStarter.Base.BusinessObjectMerger
{
    public class BusinessObjectRelationshipTraverser
    {
        //private readonly Dictionary<Type, List<string>> _traversableSingleRelationships;
        
        public BusinessObjectRelationshipTraverser()
        {
            //_traversableSingleRelationships = new Dictionary<Type, List<string>>(5);
        }

        //public Dictionary<Type, List<string>> TraversableSingleRelationships
        //{
        //    get { return _traversableSingleRelationships; }
        //}

        //public void AddSingleRelationshipToTraverse(Type businessObjectType, string relationshipName)
        //{
        //    List<string> singleRelationshipNames;
        //    if (_traversableSingleRelationships.ContainsKey(businessObjectType))
        //    {
        //        singleRelationshipNames = _traversableSingleRelationships[businessObjectType];
        //    }
        //    else
        //    {
        //        singleRelationshipNames = new List<string>();
        //        _traversableSingleRelationships.Add(businessObjectType, singleRelationshipNames);
        //    }
        //    singleRelationshipNames.Add(relationshipName);
        //}

        private bool MustTraverseRelationship(IBusinessObject bo, IRelationship relationship)
        {
            //if (relationship is IMultipleRelationship) return true;
            //if (relationship is ISingleRelationship)
            //{
            //    Type businessObjectType = bo.GetType();
            //    if (_traversableSingleRelationships.ContainsKey(businessObjectType))
            //    {
            //        List<string> singleRelationshipNames = _traversableSingleRelationships[businessObjectType];
            //        return singleRelationshipNames.Contains(relationship.RelationshipName);
            //    }
            //}
            //return false;
            RelationshipType relationshipType = relationship.RelationshipDef.RelationshipType;
            return relationshipType != RelationshipType.Association;
        }

        public delegate void BusinessObjectRelationshipVisitorDelegate(IBusinessObject businessObject, IRelationship relationship);
        public void TraverseChildren(IBusinessObject businessObject, BusinessObjectRelationshipVisitorDelegate businessObjectRelationshipVisitorDelegate)
        {
            foreach (IRelationship relationship in businessObject.Relationships)
            {
                if (!MustTraverseRelationship(businessObject, relationship)) continue;
                businessObjectRelationshipVisitorDelegate(businessObject, relationship);
            }
        }
    }
}